package com.cskaoyan.service.promotion_service;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.bean.bo.TopicBatchDeleteBO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.bean.vo.promotion_vo.TopicListVO;
import com.cskaoyan.bean.vo.promotion_vo.TopicReadVO;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/7 13:58
 */
@Service
public class TopicServiceImpl implements TopicService{
    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public TopicListVO topicList(BaseParam param, String title, String subtitle) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        PageHelper.startPage(page, limit);
        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria();
        if (title != null && !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null && !"".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        example.setOrderByClause(sort + " " + order);
        List<Topic> marketTopics = topicMapper.selectByExample(example);
        PageInfo<Topic> pageInfo = new PageInfo(marketTopics);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        TopicListVO listVO = new TopicListVO();
        listVO.setTotal(total);
        listVO.setPages(pages);
        listVO.setLimit(limit);
        listVO.setPage(page);
        listVO.setList(marketTopics);
        return listVO;
    }

    @Override
    public void deleteTopic(Topic topic) {
        topicMapper.deleteByPrimaryKey(topic.getId());
    }

    @Override
    public void batchDeleteTopic(TopicBatchDeleteBO batchDeleteBO) {
        Integer[] ids = batchDeleteBO.getIds();
        for (Integer id : ids) {
            topicMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Topic createTopic(Topic topic) {
        //TODO 响应的时候记得填入addTime和updateTime
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        int insertSelective = topicMapper.insertSelective(topic);
        if (insertSelective == 1) {
            Integer id = topicMapper.selectMaxId();
            topic.setId(id);
            return topic;
        }
        return null;
    }

    @Override
    public TopicReadVO readTopic(Integer id) {

        Topic topic = topicMapper.selectByPrimaryKey(id);
        Integer[] goodsIds = topic.getGoods();
        //根据商品id查出商品信息
        List<Goods> goodsList = new ArrayList();
        for (Integer goodsId : goodsIds) {
           Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            goodsList.add(goods);
        }
        TopicReadVO topicReadVO = new TopicReadVO();
        topicReadVO.setTopic(topic);
        topicReadVO.setGoodsList(goodsList);

        return topicReadVO;
    }

    @Override
    public Topic updateTopic(Topic topic) {
        //TODO 修改的时候改变updateTime
        int update = topicMapper.updateByPrimaryKeySelective(topic);
        topic.setUpdateTime(new Date());
        return topic;
    }
}
